//
// Created by ct on 2021/9/21.
//
#include "stdio.h"

//#include "fs_interface.h"
#include "ext2_fs.h"
#include "mymath.h"

int main_open(file_system_interface *fsi){
    char a[] = "abcd";
    fsi->pmethods->open_file(fsi->pfs,a,0);
}


int add_one_interface(math_interface * mathInterface){
   return mathInterface->pmethods->add_ond(mathInterface->pthis);
}

math_interface  int_main(int one){
    my_once  myOnce = {
            .a = one,
    };
    methods m = {
            .add_ond = (add_one_fn)add_one
    };
    math_interface anInterface = {
            .pthis = &myOnce,
            .pmethods = &m
    };
    return anInterface;
}

// 这样返回一个被回收的栈地址，会报错
//math_interface*  int_main2(int one){
//    my_once  myOnce = {
//            .a = one,
//    };
//    methods m = {
//            .add_ond = (add_one_fn)add_one
//    };
//    math_interface anInterface = {
//            .pthis = &myOnce,
//            .pmethods = &m
//    };
//    return &anInterface;
//}



int main(int argc, char *argv[]) {
    char a[] = {'a', 'b', 'c'};
    char b[10] = {};
    ext2_fs_t ext2Fs={
            .a = "001001001001001001001"
    };


    // 在这里执行了
    fs_methods_t ext2_methods = {
            .open_file = (open_file_fn)ext2_open_file,
            .read_file = (read_file_fn)ext2_read_file,
    };

    file_system_interface fsi = ext2_init(&ext2_methods, &ext2Fs);

    printf(" \n %s  \n", "----------");

  //  fsi.pmethods->open_file(&ext2Fs,a,0);
    main_open(&fsi);
    fsi.pmethods->read_file(&ext2Fs,0,b,10);

    printf("\n b : %s  \n --- ",b);
  //  fsi.pmethods->read_file;


      my_once mo = {
              .a = 1,
      };

    math_interface mi =  init_myonce(&mo);

    add_one_interface(&mi);

    printf("%d ",mo.a);
    printf("%d ",((my_once  *)mi.pthis)->a);


    math_interface mi2 = int_main(1);

    add_one_interface(&mi2);

   // printf("%d ",mo.a);
    printf("\n mi2 ---- %d ",((my_once  *)mi2.pthis)->a);

    // 这样调用会报错，因为栈内变量被释放，无法返回指针
//    math_interface * mi3 = int_main2(1);
//
//    add_one_interface(mi3);
//
//    // printf("%d ",mo.a);
//    printf("\n  ---- %d ",((my_once  *)mi3->pthis)->a);

}
